package register.show.users;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.eclipse.jdt.internal.compiler.ast.ThisReference;
import org.jdom.Element;
import org.omg.CORBA.PUBLIC_MEMBER;

import register.pojo.User;

public class ShowUsers {
	public static Element showUsers(List<User> users) throws UnsupportedEncodingException
	{
		return getHtml(users);
	}
	public static Element getHtml(List<User> users) throws UnsupportedEncodingException
	{
		Element root=new Element("html");
		Element head=new Element("head");
		Element meta=new Element("meta");
		Element body=new Element("body");
		Element head2=new Element("h2");
		Element div=new Element("div");
		Element table=new Element("table");
		Element tr=new Element("tr");
		Element td1=new Element("td");
		Element td2=new Element("td");
		Element td3=new Element("td");
		Element td4=new Element("td");
		Element td5=new Element("td");
		
		meta.setAttribute("charset","UTF-8");
		head2.setAttribute("style","text-align:center");
		head2.setText("User Table");
		div.setAttribute("align", "center");
		tr.setAttribute("bgcolor","cornflowerblue");
		td1.setText("Username");
		td2.setText("Salary");
		td3.setText("Sex");
		td4.setText("Birthday");
		td5.setText("HeadProtroit");
		
		root.addContent(head);
		root.addContent(body);
		head.addContent(meta);
		
		body.addContent(head2);
		body.addContent(div);
		div.addContent(table);
		table.addContent(tr);
		tr.addContent(td1);
		tr.addContent(td2);
		tr.addContent(td3);
		tr.addContent(td4);
		tr.addContent(td5);
		addUser(users, table);
		return root;
		
	}
	
	public static void addUser(List<User> users,Element table) throws UnsupportedEncodingException
	{
		for(int i=0;i<users.size();i++)
		{
			String[] userAttributes=users.get(i).toString().split(",");
			Element tr=new Element("tr");
			Element td1=new Element("td");
			Element td2=new Element("td");
			Element td3=new Element("td");
			Element td4=new Element("td");
			Element td5=new Element("td");
			td1.setText(new String(userAttributes[0].split("=")[1].getBytes(),"UTF-8"));
			td2.setText(userAttributes[1].split("=")[1]);
			td3.setText(userAttributes[2].split("=")[1]);
			td4.setText(userAttributes[3].split("=")[1]);
			td5.setText(userAttributes[4].split("=")[1]);
			tr.addContent(td1);
			tr.addContent(td2);
			tr.addContent(td3);
			tr.addContent(td4);
			tr.addContent(td5);
			table.addContent(tr);
		}
	}
}
